package com.database.daos.imp;

import javax.sql.DataSource;

import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;

import com.database.daos.ICountDao;
import com.database.objects.*;

public class CountDao extends GenericDao<Long> implements ICountDao {

	private static final long serialVersionUID = 8813668549164636239L;

	private SimpleJdbcTemplate jdbcTemplate;

	@Override
	public Long getCountElementsByDeveloperAndDateAndTypeAndStatus(Developer developer, String date, Type type, Status status) {
		String query = "";
		String queryTotal = "";
		if (type.isCommit()) {
			query = "select count(com_id) as counter from commit where dev_id = ? and com_date = ? and com_status = ?";
			queryTotal = "select count(com_id) as counter from commit where dev_id = ? and com_date = ? and com_status != 'UNKNOWN'";
		} else if (type.isFileOO()) {
			query = "select count(fil_id) as counter from file_oo natural inner join file natural inner join commit where dev_id = ? and com_date = ? and fil_status = ?";
			queryTotal = "select count(fil_id) as counter from file_oo natural inner join file natural inner join commit where dev_id = ? and com_date = ? and fil_status != 'UNKNOWN'";
		} else if (type.isFileNoOO()) {
			query = "select count(fil_id) as counter from file_no_oo natural inner join file natural inner join commit where dev_id = ? and com_date = ? and fil_status = ?";
			queryTotal = "select count(fil_id) as counter from file_no_oo natural inner join file natural inner join commit where dev_id = ? and com_date = ? and fil_status != 'UNKNOWN'";
		} else if (type.isClass()) {
			query = "select count(cla_id) as counter from class natural inner join file natural inner join commit where dev_id = ? and com_date = ? and cla_status = ?";
			queryTotal = "select count(cla_id) as counter from class natural inner join file natural inner join commit where dev_id = ? and com_date = ? and cla_status != 'UNKNOWN'";
		} else if (type.isMethod()) {
			query = "select count(met_id) as counter from method natural inner join class natural inner join file natural inner join commit where dev_id = ? and com_date = ? and met_status = ?";
			queryTotal = "select count(met_id) as counter from method natural inner join class natural inner join file natural inner join commit where dev_id = ? and com_date = ? and met_status != 'UNKNOWN'";
		} else if (type.isLineOO()) {
			query = "select count(lio_id) as counter from line_oo natural inner join method natural inner join class natural inner join file natural inner join commit where dev_id = ? and com_date = ? and lio_status = ?";
			queryTotal = "select count(lio_id) as counter from line_oo natural inner join method natural inner join class natural inner join file natural inner join commit where dev_id = ? and com_date = ? and lio_status != 'UNKNOWN'";
		} else if (type.isTask()) {
			query = "select count(iss_id) as counter from issue where iss_type = 'TASK' and dev_id = ? and iss_date = ? and iss_status = ?";
			queryTotal = "select count(iss_id) as counter from issue where iss_type = 'TASK' and dev_id = ? and iss_date = ? and iss_status != 'UNKNOWN'";
		} else if (type.isEnhancement()) {
			query = "select count(iss_id) as counter from issue where iss_type = 'ENHANCEMENT' and dev_id = ? and iss_date = ? and iss_status = ?";
			queryTotal = "select count(iss_id) as counter from issue where iss_type = 'ENHANCEMENT' and dev_id = ? and iss_date = ? and iss_status != 'UNKNOWN'";
		} else if (type.isDefect()) {
			query = "select count(iss_id) as counter from issue where iss_type = 'DEFECT' and dev_id = ? and iss_date = ? and iss_status = ?";
			queryTotal = "select count(iss_id) as counter from issue where iss_type = 'DEFECT' and dev_id = ? and iss_date = ? and iss_status != 'UNKNOWN'";
		}

		if (!query.equals("") && !queryTotal.equals("")) {
			if (status.isTotal()) {
				return jdbcTemplate.queryForLong(queryTotal, developer.getId(), date);
			} else {
				return jdbcTemplate.queryForLong(query, developer.getId(), date, status.toString());
			}
		}
		return 0L;
	}

	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new SimpleJdbcTemplate(dataSource);
	}

}